import json
import re

from bs4 import BeautifulSoup
from flask import Blueprint, request, jsonify

import models
from exts import get_html_by_selenium, db

bp = Blueprint("crawl_api", __name__, url_prefix="/api/crawl")


@bp.route("/crawl_elm_keyword", methods=["GET"])
def crawl_elm_keyword():
    # keyword = request.values.get("keyword")
    url = '''https://h5.ele.me/2021001185671035/pages/ele-takeout-index/ele-takeout-index?storeTransmit=%7B%22trace_id%22%3A%2221283d9917118102806945174ed6dd%22%2C%22anchorScene%22%3A%22BLANK%22%2C%22subsubchannel%22%3A%22mobile.default.alsc-rec-data-center%3ATpp-26736%22%7D&menu_extra_info=%7B%22is_anchor%22%3Afalse%2C%22name_desc%22%3A%22%E5%88%9A%E5%88%9A%E7%9C%8B%E8%BF%87%22%2C%22skip_select_sku%22%3Atrue%2C%22group_name%22%3A%22%E5%88%9A%E5%88%9A%E7%9C%8B%E8%BF%87%22%2C%22sku_ids%22%3A%5B%22300000298775883861%22%5D%2C%22popularity%22%3A0%2C%22type%22%3A%22CUSTOM%22%7D&shopId=E941894719032918923&cartTransmit=%7B%22trace_id%22%3A%2221283d9917118102806945174ed6dd%22%2C%22type%22%3A1%2C%22scheme_type%22%3A%22ITEM_STICKY_CATEGORY_SCHEME%22%2C%22subsubchannel%22%3A%22mobile.default.alsc-rec-data-center%3ATpp-26736%22%7D&geohash=ws02t3dmhsc&brandId=0&o2o_extra_param=%7B%22rank_id%22%3A%225b0a39337d3e4a77a51d3c3507b732ad%22%7D&spm=a2f6g.13154471.searchFoodList.5&keyword=%E5%A5%B6%E8%8C%B6&rank_id=5b0a39337d3e4a77a51d3c3507b732ad&refer=%E5%8E%86%E5%8F%B2%E8%AF%8D&content_track=%257B%2522traceId%2522%253A%252221283d9917118102806945174ed6dd%2522%252C%2522rank_id%2522%253A%25225b0a39337d3e4a77a51d3c3507b732ad%2522%252C%2522openingShopAmount%2522%253A201%252C%2522lbehavor_biztype%2522%253A%2522waimai%2522%252C%2522biz_type%2522%253A%25220%2522%252C%2522statusWeight%2522%253A5%252C%2522aoi_id%2522%253A200360101%252C%2522restingShopAmount%2522%253A74%252C%2522search_bid%2522%253A%25220.0%2522%252C%2522tpp_buckets%2522%253A%252226551%25230%2523235060%252315_26551%252314808%2523468845%25236_26551%252319181%2523464453%25237_26551%252322935%2523470538%252320_26551%252320569%2523466369%25236_18537%25230%2523382172%25236_18537%252324621%2523475026%25232%2522%252C%2522scene%2522%253A%2522SEARCH_BROWSER%2522%257D&page_type=1&from=mobile.default&fromVersion=11.8.1&guideTrack=%25257B%252522search_ab_bucket%252522%25253A%252522a%252522%25252C%252522search_tpp_buckets%252522%25253A%25252222816%2525230%252523376919%25252315_18536%2525230%252523381774%25252326_18536%25252311865%252523444408%2525239_18536%25252311759%252523444210%252523628_18536%25252310700%252523439646%2525237_18536%25252324773%252523475749%2525238_18536%25252313832%252523453014%25252320%252522%25252C%252522search_app_id%252522%25253A22816%25252C%252522alscJointAbTag%252522%25253A%252522unknow%252522%25252C%252522search_trace_id%252522%25253A%2525222135e5ba17117177597014558eaf98%252522%25252C%252522personalizedDisable%252522%25253Afalse%25252C%252522elemAbTest%252522%25253A%25257B%252522sugBackwardSkipMiddlePageCache%252522%25253Afalse%25257D%25252C%252522search_rank_id%252522%25253A%252522a796ef4c12984aa2a7bd7837da6e0ef6%252522%25252C%252522pageAlgoExtend%252522%25253A%252522%2525257B%2525257D%252522%25257D&longitude=113.08556&latitude=22.597015&spm-pre=a2ogi.13162730.zebra-ele-login-module-9089118186'''
    # cookies_str = '''ubt_ssid=452d1k2gpwz0yrprbyan5wvf81rvjdi2_2024-03-05; cna=oMdtHoEWrTcCAbcOhcsEa/gM; mtop_partitioned_detect=1; _m_h5_tk=732d4e94dce6840dbccf62e021772acf_1709637169436; _m_h5_tk_enc=6edcc7c9d027e9f49b8df08616a043d1; _samesite_flag_=true; cookie2=10b749401c6e31178c00d82c7ba13191; t=5d8008daea26839658c08045afb167f2; _tb_token_=ef7a1538d38e5; xlly_s=1; sgcookie=E100%2BrEDMDGLc80hORqESHuY9sL%2FKD1c7Vc0cTIF1ns%2F4OZ3VTXvlY6dD7Mt0WmoDKWpeXfl%2BAMDQxlR1sksufnrmMzuuIzfAlRmPMoVHawlgq4%3D; unb=2206134435636; munb=2206134435636; csg=0b7a0c56; t_eleuc4=id4=0%40BA%2FvuHCrrRtUKcLT6lrugKUvzYwf8tYJqJsbKg%3D%3D; USERID=5627133562; SID=MTBiNzQ5NDAxYzZlMzExNzhjMDBkODJjN2JhMTMxOTEsbTxzTtrAftljhvILmrPh; UTUSER=5627133562; app_cityid=215; tfstk=eV8yjeNiiwvX_dV64ibFbg7Yv9QRWNH6re6CtBAhNTXkR_x48KpNxgGRreue3LzWV0ICLWWDO3a5Np13wEJaRzM-2B4FRwDsCVg6w__d-AG-zlBBwogNCLus5QdJk1c_qVG8UF_1x_pYsvUlbfARnETKRpn74QB4-yPFagXiWtz3-E5ygOAovy4Hu_SP4XydiM6HJe3LYgfAgOGqgQSvaj1WcViYvkIR_sWsitqLvgfAgOGqgkEd215VCjXc.; isg=BPf3nqYQXihIxdqk4ErU7vkwhu1BvMseMywuE0mkE0Yt-Bc6UYxbbrXe3l6mEKOW'''
    # cookies = {i.split("=")[0].replace(" ", ""): i.split("=")[1].replace(" ", "") for i in cookies_str.split(";")}
    page_source = get_html_by_selenium(url=url)
    models_page = models.Pages(
        url=url,
        page=page_source,
        page_type=1
    )
    db.session.add(models_page)
    db.session.commit()
    return jsonify({"success": True})


@bp.route("/page_source_list", methods=["GET"])
def page_source_list():
    def to_dict(item):
        return {
            "id": item.id,
            "create_time": item.create_time.strftime("%Y-%m-%d, %H:%M:%S"),
            "url": item.url
        }

    db_res = db.session.query(models.Pages).with_entities(
        models.Pages.id,
        models.Pages.create_time,
        models.Pages.url,
        models.Pages.extends
    ).all()
    return list(
        map(to_dict, db_res)
    )


@bp.route("/page_source", methods=["GET"])
def page_source():
    page_id = request.values.get("page_id")
    first = db.session.query(models.Pages).filter(models.Pages.id == page_id).first()
    if first:
        return first.page
    return ""


@bp.route("/analysis_data", methods=["GET"])
def analysis_data():
    page_id = request.values.get("page_id")
    first = db.session.query(models.Pages).filter(models.Pages.id == page_id).first()
    if first:
        page_source = first.page
        bs = BeautifulSoup(page_source, "html.parser")  # 解析文档，用html的解析器

        items = bs.select('tiga-view[class="menuItem--info--box"]')
        for item in items:
            title_tag = item.select('tiga-view[class="menuItem--info-title--warp"]')
            sales_volume_tag = item.select('tiga-view[class="menuItem--info-sales"]')
            price_tag = item.select('tiga-view[class="menuItem--info-bottom"]')
            if not title_tag or not sales_volume_tag or not price_tag:
                continue
            title = title_tag[0].get_text()
            sales_volume = re.findall("\d+.?\d+", sales_volume_tag[0].get_text())
            price = re.findall("\d+.?\d+", price_tag[0].get_text())

            if title and sales_volume and price:
                try:
                    int(sales_volume[0])
                    int(price[0])
                except:
                    continue

                mc = models.Commodity(
                    extends=json.dumps({
                        "nodes": []
                    }),
                    name=title,
                    price=int(price[0]),
                    sales_volume=int(sales_volume[0])
                )
                db.session.add(mc)
                db.session.commit()

        # items = bs.select('tiga-view[class="shopmode-dragfood-item"]')
        # for item in items:
        #     nodes_tag = item.select("tiga-rich-text[class=\"shopmode-food-item-des\"]")
        #     if len(nodes_tag) > 0:
        #         nodes = (nodes_tag[0] or {}).get("nodes", [])
        #         if not nodes:
        #             continue
        #     else:
        #         continue
        #     price_tag = item.select("tiga-view[class=\"text-tag-selector\"]")
        #     if len(price_tag) > 2:
        #         price = price_tag[1].get_text()
        #     else:
        #         continue
        #     nodes = json.loads(nodes)
        #     name = ""
        #     for child in nodes:
        #         children = child["children"]
        #         for child in children:
        #             name += child["text"]
        #     mc = models.Commodity(
        #         extends=json.dumps({
        #             "nodes": nodes
        #         }),
        #         name=name,
        #         price=price
        #     )
        #     db.session.add(mc)
        #     db.session.commit()
        return jsonify({"success": True})
    return jsonify({"success": False})
